﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Imports System.Xml

Namespace CMISObjects
   <XmlRoot("bulkUpdate", Namespace:=CmisNamespaceConstants.Cmisra)>
   Public Class CmisBulkUpdateType
      Implements IXmlSerializable
      Public Sub New()

      End Sub


      Public Property ObjectIdAndChangeToken As CmisObjectIdAndChangeTokenType()
      Public Property Properties As CMISObjectModel.Properties.ICMISProperty()
      Public Property AddSecondaryTypeIds As String()
      Public Property RemoveSecondaryTypeIds As String()



      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml

         Dim doc As New System.Xml.XmlDocument()
         doc.Load(reader)
         Dim node As XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, Common.GetXMLRootName(GetType(CmisBulkUpdateType)))

         Dim changeToken As New List(Of CMISObjectModel.CMISObjects.CmisObjectIdAndChangeTokenType)
         Dim addSecondaryTypeIds As New List(Of String)
         Dim removeSecondaryTypeIds As New List(Of String)

         For Each child As XmlNode In node.ChildNodes
            Dim lname As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lname.Equals("objectidandchangetoken") Then
               Dim serializer As New XmlSerializer(GetType(CMISObjectModel.CMISObjects.CmisObjectIdAndChangeTokenType))
               Using stream As New IO.StringReader(child.OuterXml)
                  changeToken.Add(DirectCast(serializer.Deserialize(stream), CMISObjectModel.CMISObjects.CmisObjectIdAndChangeTokenType))
               End Using
            ElseIf lname.Equals("properties") Then
               Me.Properties = Common.ParsePropertiesNode(child)
            ElseIf lname.Equals("addsecondarytypeids") Then
               addSecondaryTypeIds.Add(child.InnerText)
            ElseIf lname.Equals("removesecondarytypeids") Then
               removeSecondaryTypeIds.Add(child.InnerText)
            End If
         Next
         Me.ObjectIdAndChangeToken = changeToken.ToArray
         Me.AddSecondaryTypeIds = addSecondaryTypeIds.ToArray
         Me.RemoveSecondaryTypeIds = removeSecondaryTypeIds.ToArray
      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml

         If ObjectIdAndChangeToken IsNot Nothing Then
            For Each item As CmisObjectIdAndChangeTokenType In ObjectIdAndChangeToken
               Dim serializer As New XmlSerializer(item.GetType)
               serializer.Serialize(writer, item)
            Next
         End If
         If Me.Properties IsNot Nothing Then
            writer.WriteStartElement("properties", CmisNamespaceConstants.Cmis)
            For Each cmisProperty As CMISObjectModel.Properties.ICMISProperty In Me.Properties
               Dim serializer As New XmlSerializer(cmisProperty.GetType)
               serializer.Serialize(writer, cmisProperty)
            Next
            writer.WriteEndElement() 'Element: properties
         End If
         If AddSecondaryTypeIds IsNot Nothing Then
            For Each item As String In AddSecondaryTypeIds
               writer.WriteElementString("addSecondaryTypeIds", CmisNamespaceConstants.Cmis, item)
            Next
         End If
         If RemoveSecondaryTypeIds IsNot Nothing Then
            For Each item As String In RemoveSecondaryTypeIds
               writer.WriteElementString("removeSecondaryTypeIds", CmisNamespaceConstants.Cmis, item)
            Next
         End If
      End Sub
   End Class
End Namespace